﻿using Infrastructure.Attribute;
using Infrastructure.Extensions;
using ZR.Repository;
using System.Linq;
using ZR.Service.IService.ProductionControl;
using ZR.Model.Models.ProductionControl;
using ZR.Model.Dto.Business.ProductionControl;
using ZR.Model;

namespace ZR.Service.Service.ProductionControl
{
    /// <summary>
    /// 夹具信息Service业务层处理
    /// </summary>
    [AppService(ServiceType = typeof(ICksFixtureinfoService), ServiceLifetime = LifeTime.Transient)]
    public class CksFixtureinfoService : BaseService<CksFixtureinfo>, ICksFixtureinfoService
    {
        /// <summary>
        /// 查询夹具信息列表
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        public PagedInfo<CksFixtureinfoDto> GetList(CksFixtureinfoQueryDto parm)
        {
            var predicate = QueryExp(parm); 
            
            var response = Queryable()
                .Where(predicate.ToExpression())
                .OrderByDescending(a=>a.UpdateTime)
                .ToPage<CksFixtureinfo, CksFixtureinfoDto>(parm);

            return response;
        }


        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public CksFixtureinfo GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();

            return response;
        }

        /// <summary>
        /// 添加夹具信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public CksFixtureinfo AddCksFixtureinfo(CksFixtureinfo model)
        {
            model.MaxUsageCount = 200;
            return Insertable(model).ExecuteReturnEntity();
        }

        /// <summary>
        /// 修改夹具信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateCksFixtureinfo(CksFixtureinfo model)
        {
            return Update(model, true);
        }

        /// <summary>
        /// 查询导出表达式
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        private static Expressionable<CksFixtureinfo> QueryExp(CksFixtureinfoQueryDto parm)
        {
            var predicate = Expressionable.Create<CksFixtureinfo>();
            predicate.AndIF(parm.FixtureId > 0, (a) => a.FixtureId == parm.FixtureId);
            return predicate;
        }

        public int Reset(int[] ids, string title = "")
        {

          return  Context.Updateable<CksFixtureinfo>()
                .SetColumns(a => a.UsageCount,0)
                .Where(a=>ids.Contains(a.Id))
                .ExecuteCommand();
        }
    }
}